/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.controller;

import com.shop.cereshop.business.annotation.NoRepeatSubmit;
import com.shop.cereshop.business.param.shop.IdCardOCRParam;
import com.shop.cereshop.business.param.shop.ShopQualificationParam;
import com.shop.cereshop.business.service.shop.CereShopQualificationFlowService;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.domain.im.tx.resp.ocr.IdCardOCRCombineResp;
import com.shop.cereshop.commons.domain.im.tx.resp.ocr.IdCardOCRResp;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.ocr.TencentOCRService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 店铺资质认证流程控制器
 */
@RestController
@RequestMapping("qualificationFlow")
@Slf4j(topic = "ShopQualificationFlowController")
@Api(value = "店铺资质认证流程模块", tags = "店铺资质认证流程模块")
public class ShopQualificationFlowController {

    @Autowired
    private CereShopQualificationFlowService cereShopQualificationFlowService;

    @Autowired
    private TencentOCRService tencentOCRService;

    /**
     * 开始资质认证流程
     */
    @PostMapping(value = "start")
    @NoRepeatSubmit
    @ApiOperation(value = "开始资质认证流程")
    public Result<String> startQualificationFlow(@RequestBody @Validated ShopQualificationParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        String flowId = cereShopQualificationFlowService.startQualificationFlow(param, user);
        return Result.success(flowId);
    }

    /**
     * OCR识别营业执照
     */
    @PostMapping(value = "recognizeLicense")
    @ApiOperation(value = "OCR识别营业执照")
    public Result<Object> recognizeBusinessLicense(
            @ApiParam(value = "营业执照图片URL", required = true) @RequestParam String imageUrl,
            @ApiParam(value = "认证流程ID", required = true) @RequestParam String flowId) throws CoBusinessException {
        Object result = cereShopQualificationFlowService.recognizeBusinessLicense(imageUrl, flowId);
        return Result.success(result);
    }

    /**
     * 提交身份证人脸识别
     */
    @PostMapping(value = "faceRecognition")
    @NoRepeatSubmit
    @ApiOperation(value = "提交身份证人脸识别")
    public Result<Object> submitFaceRecognition(
            @ApiParam(value = "姓名", required = true) @RequestParam String name,
            @ApiParam(value = "身份证号", required = true) @RequestParam String idCardNumber,
            @ApiParam(value = "人脸照片URL", required = true) @RequestParam String faceImageUrl,
            @ApiParam(value = "认证流程ID", required = true) @RequestParam String flowId) throws CoBusinessException {
        Object result = cereShopQualificationFlowService.submitFaceRecognition(name,idCardNumber, faceImageUrl, flowId);
        return Result.success(result);
    }

    /**
     * 身份证OCR识别（正反面）
     */
    @PostMapping(value = "idCardOCR")
    @ApiOperation(value = "身份证OCR识别（正反面）")
    public Result<IdCardOCRCombineResp> idCardOCR(@RequestBody @Validated IdCardOCRParam param) throws CoBusinessException {
        IdCardOCRResp front = tencentOCRService.idCardOCRByUrl(param.getFrontImageUrl(), "FRONT");
        IdCardOCRResp back = tencentOCRService.idCardOCRByUrl(param.getBackImageUrl(), "BACK");
        IdCardOCRCombineResp resp = new IdCardOCRCombineResp();
        resp.setFront(front);
        resp.setBack(back);
        return Result.success(resp);
    }

    /**
     * 完成资质认证并创建店铺
     */
    @PostMapping(value = "complete")
    @NoRepeatSubmit
    @ApiOperation(value = "完成资质认证并创建店铺")
    public Result<Object> completeQualificationAndCreateShop(
            @ApiParam(value = "认证流程ID", required = true) @RequestParam String flowId,
            HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        Object result = cereShopQualificationFlowService.completeQualificationAndCreateShop(flowId, user);
        return Result.success(result);
    }

    /**
     * 获取认证流程状态
     */
    @GetMapping(value = "status/{flowId}")
    @ApiOperation(value = "获取认证流程状态")
    public Result<Object> getQualificationFlowStatus(
            @ApiParam(value = "认证流程ID", required = true) @PathVariable String flowId) throws CoBusinessException {
        Object result = cereShopQualificationFlowService.getQualificationFlowStatus(flowId);
        return Result.success(result);
    }

    /**
     * 取消认证流程
     */
    @PostMapping(value = "cancel")
    @NoRepeatSubmit
    @ApiOperation(value = "取消认证流程")
    public Result<String> cancelQualificationFlow(
            @ApiParam(value = "认证流程ID", required = true) @RequestParam String flowId,
            HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        String result = cereShopQualificationFlowService.cancelQualificationFlow(flowId, user);
        return Result.success(result);
    }


}
